<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<script type="text/javascript">
//var fileFlag = $("#fileFlag");

	$(document).ready(function(){
		//fileFlag = $("#fileFlag").val();
		
		$("#updateBtn").click(function(){
			$("#updateForm").submit();
		});
		
		var clickCount=0;  
		$("#addFile").click(function(){
			//alert("test 파일추가");
			clickCount++;
			if(clickCount==1){
				document.getElementById("test").style.display="block";
				clickCount++;  //2
				//alert(clickCount);  
			}else if(clickCount==3){
				alert(clickCount);
				document.getElementById("test2").style.display="block";
				clickCount++;
			}else{
				alert("업로드는 최대 3개까지 가능합니다.");
			}
		}); //파일 추가 버튼 클릭
		
	});//ready
	
	var fileNo =0;
	function deleteFile(count){
		if(!confirm("한번 지운 파일은 복구하실 수 없습니다. 지우시겠습니까?")){
			return false;
		}
		fileNo=$("input[name=fileNo]").val();
		//fileFlag="true"; //delete를 누를 경우 flag를 true로 바꾼다.
		var no = $("input[name=no]").val();
		if(count==1){
			var org = $("input[name=deleteFileSaveList1]").val();
			$.ajax({
				type:"POST",
				url:"deleteFile.inven",
				data:"no="+no+"&orgFileName="+org,
			    dataType:"json",
			    success:function(jsonData){
			    	$("#fileListView1").html("<input type='file' name='file["+fileNo+"]' id='file"+fileNo+"'>");
					fileNo+=1;
			    }//success
			});//ajax
			
		}else if(count==2){
			var org = $("input[name=deleteFileSaveList2]").val();
			$.ajax({
				type:"POST",
				url:"deleteFile.inven",
				data:"no="+no+"&orgFileName="+org,
			    dataType:"json",
			    success:function(jsonData){
			    	$("#fileListView2").html("<input type='file' name='file["+fileNo+"]' id='file"+fileNo+"'>");
					fileNo+=1;
			    }//success
			});//ajax
		}else if(count==3){
			var org = $("input[name=deleteFileSaveList3]").val();
			$.ajax({
				type:"POST",
				url:"deleteFile.inven",
				data:"no="+no+"&orgFileName="+org,
			    dataType:"json",
			    success:function(jsonData){
			    	$("#fileListView3").html("<input type='file' name='file["+fileNo+"]' id='file"+fileNo+"'>");
					fileNo+=1;
			    }//success
			});//ajax
		}
	}
</script>


<%-------------------- 글 수정 폼 --------------------%>

<h4 align="center">글 수정하기</h4>
<form action="updateContent.inven" method="post" id="updateForm" enctype="multipart/form-data">
<input type="hidden" name="no" value="${requestScope.bvo.no}">
	<table>
		<tr>
			<td>No.${requestScope.bvo.no}</td>
			<td colspan="2"><input style="width: 600px; height: 30px;" type="text" name="title" value="${requestScope.bvo.title }"></td>
		</tr>
		<tr>
		<td style="width: 5%;">${requestScope.bvo.id }</td>
		<td style="width: 60%;">&nbsp;</td>
		<td style="width: 35%;">${requestScope.bvo.timePosted } Hits ${requestScope.bvo.hits }</td>
	</tr>
	</table>

<%----------------------- 파일 다운로드 부분 -----------------------%>
<hr>
<input type="hidden" id="fileFlag" value="false">
<div id="updateView">
<c:choose>
<c:when test="${requestScope.saveList!=null }">
	<c:forEach varStatus="status" items="${requestScope.orgList}" var="orgList">
		<div id="fileListView${status.count}">${orgList }
		<input type="hidden" name="deleteFileSaveList${status.count}" value="${orgList }">
		<a href="javascript:deleteFile(${status.count })">삭제</a></div>
	</c:forEach>
	<c:choose>
		<c:when test="${fn:length(requestScope.saveList)==1}">
			<input type="file" name="file[0]" id="file0">
			<input type=file name=file[1] id="file1">
			<input type="hidden" name="fileNo" value="2">
		</c:when><c:when test="${fn:length(requestScope.saveList)==2}">
			<input type="file" name="file[0]" id="file0">
			<input type="hidden" name="fileNo" value="1">
		</c:when>
	</c:choose>
</c:when>
<c:otherwise>
	<div id="viewFile1"><input type="file" name="file[0]" id="file0"><input type="button" id="addFile" value="파일추가"></div>
	<div id="test" style="display: none;"><input type=file name=file[1] id="file1"></div>
	<div id="test2" style="display: none;"><input type=file name=file[2] id="file2"></div>
</c:otherwise>
</c:choose>
</div>

<%-- 글 내용 보여주는 부분 --%>
<br><textarea cols="90" rows="20" name="content" style="font-family: NanumGothic; width: 100%;">${requestScope.bvo.content }</textarea><br>
<input type="button" id="updateBtn" value="수정하기">
</form>

</body>
